package com.aim.controller;

import com.aim.common.constant.Syscst;
import com.aim.common.entity.MenuNode;
import com.aim.common.service.MenuNodeService;
import com.aim.common.util.JdbcTemplateUtil;
import com.aim.common.util.SystemUtil;
import com.aim.common.util.WebUtil;
import com.aim.config.shiro.ShiroKit;
import com.aim.config.shiro.ShiroUser;
import com.aim.entity.system.SysLoginLog;
import com.aim.entity.system.SysNotice;
import com.aim.entity.system.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @AUTO 默认控制器
 * @Author AIM
 * @DATE 2019/4/18
 */
@Controller
public class IndexController {

    private static String PREFIX = "index";

    @Autowired
    private MenuNodeService menuService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = {"/welcome.html", "/welcome"})
    public String welcome(HttpServletRequest request, Model model) {
        SysUser user = ShiroKit.getUserNotNull();
        SysLoginLog sysLoginLog = null;
        if (user != null) {
            Object[] param = {user.getUserId()};
            StringBuffer sb = new StringBuffer();
            sb.append("SELECT * FROM `sys_login_log` WHERE `USER_ID`=?  ORDER BY `LOGIN_LOG_ID` DESC LIMIT 1");
            RowMapper<SysLoginLog> rm = BeanPropertyRowMapper.newInstance(SysLoginLog.class);
            try {
                sysLoginLog = JdbcTemplateUtil.queryForBean(jdbcTemplate, sb.toString(), param, SysLoginLog.class);
                model.addAttribute("ip", sysLoginLog.getIpAddress());
                model.addAttribute("lastDate", sysLoginLog.getCreateTime());
            } catch (DataAccessException e) {
            }
            sb.setLength(0);
            sb.append("SELECT COUNT(0) AS total FROM `sys_login_log` WHERE `USER_ID`=? ");
            Integer total = JdbcTemplateUtil.queryForObject(jdbcTemplate, sb.toString(), Integer.class, user.getUserId());
            model.addAttribute("loginCount", total);
            sb.setLength(0);
            sb.append("SELECT `VALUE` AS startDate FROM `sys_config` WHERE `KEY`=?");
            String startDate = JdbcTemplateUtil.queryForObject(jdbcTemplate, sb.toString(), String.class, Syscst.SYSTEM_TIME);
            model.addAttribute("startDate", startDate);
            sb.setLength(0);
            sb.append("SELECT `TITLE`,`CONTENT` FROM `sys_notice` ORDER BY `NOTICE_ID` LIMIT 10");
            List<SysNotice> notices = JdbcTemplateUtil.queryForListBean(jdbcTemplate, sb.toString(), SysNotice.class);
            model.addAttribute("notices", notices);
        }
        model.addAttribute("os", SystemUtil.getOsInfo());
        model.addAttribute("port", WebUtil.getPort(request));
        return "welcome";
    }

    /**
     * 跳转首页
     */
    @RequestMapping(value = {"/", "/index.html", "/index"})
    public String index(Model model) {
        //获取当前用户角色列表
        ShiroUser user = ShiroKit.getUserNotNull();
        if (user != null) {
            model.addAttribute("username", user.getName());
            model.addAttribute("uid", user.getUserId());
            List<Long> roleList = user.getRoleList();
            String roleName = user.getRoles().get(user.getRoleId());
            model.addAttribute("roleName", roleName);
            if (roleList == null || roleList.size() == 0) {
                ShiroKit.getSubject().logout();
                model.addAttribute("tips", "该用户没有角色，无法登陆");
                return "/login";
            }

            List<MenuNode> menus = menuService.getUserMenuNodes(roleList);
            model.addAttribute("menus", menus);
        }
        return PREFIX;
    }
}
